#include "fuse.h"

int tagfs_getattr(const char *path, struct stat *stbuf){

	if (fs->get_stat(path, stbuf) != 0)
		return -ENOENT;
	else
		return 0;

    /*if (strcmp(path, "/") == 0) {
            stbuf->st_mode = S_IFDIR | 0755;
            stbuf->st_nlink = 2;
	    
    } else {
	if (Tag::is_valid(path)) {
            stbuf->st_mode = S_IFDIR | 0755;
            stbuf->st_nlink = 2;
	    stbuf->st_size = 4096;
	} else if(strcmp(path, corrupted_db) == 0) {
            stbuf->st_mode = S_IFREG | 0444;
            stbuf->st_nlink = 1;
    	    stbuf->st_size = strlen(db_error_msg);
	}
	else {
            res = -ENOENT;
	}
    }*/

}

int tagfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi){

    /*(void) offset;
    (void) fi;*/
    vector<string> *result = NULL;
																		     
    filler(buf, ".", NULL, 0);
    filler(buf, "..", NULL, 0);

	if ((result = fs->get_children(path)) == NULL)
		return -1;
	for (uint i = 0; i < result->size(); i++)
		filler(buf, (*result)[i].c_str(), NULL, 0);
		
    return 0;
}

int tagfs_mkdir(const char *path, mode_t mode){
	string new_path(path);
	return fs->add_dir(new_path);
};

int tagfs_rmdir(const char *path){
	string new_path(path);
	return fs->del_dir(new_path);
};

/*static int tagfs_open(const char *path, struct fuse_file_info *fi)
{

     if(strcmp(path, corrupted_db) != 0)
         return -ENOENT;

    if((fi->flags & 3) != O_RDONLY)
         return -EACCES;

     return 0;
}
																							 
static int tagfs_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
{

   size_t len;
  (void) fi;
   if(strcmp(path, corrupted_db) != 0)
       return -ENOENT;

   len = strlen(db_error_msg);
   if (offset < len) {
       if (offset + size > len)
           size = len - offset;
       memcpy(buf, db_error_msg + offset, size);
   } else
	size = 0;

    return size;
}
*/

int tagfs_mknod(const char *path, mode_t mode, dev_t dev) {
	string new_path(path);
	return fs->add_file(new_path);
};

int tagfs_read(const char *path, 
		char *buf, 
		size_t size, 
		off_t offset, 
		struct fuse_file_info *fi) {
	string new_path(path);
	int ret = fs->read_file(new_path, buf, size, offset);
	fprintf(stderr, "=@%s@=\n", buf); fflush(stderr);
	cout << "[tagfs_read] Return value" << ret << endl;
	return ret;
};

int tagfs_unlink(const char *path){
	return fs->delete_file(path);
};

int tagfs_write(const char *path, 
		 const char *buf, 
		 size_t size, 
		 off_t offset, 
		 struct fuse_file_info *fi) {
	string new_path(path);
	return fs->write_file(new_path, buf, size, offset);
};

/* stubs */
int tagfs_flush(const char *path, struct fuse_file_info *) {
	return 0;
};

int tagfs_setxattr(const char *, const char *, const char *, size_t, int){
	return 0;
};

int tagfs_utimens(const char *path, const struct timespec tv[2]){
	string new_path(path);
	return fs->utime_file(new_path, tv);
};

int tagfs_chmod(const char *, mode_t) {
	return 0;
};

int tagfs_chown(const char *, uid_t, gid_t) {
	return 0;
};

int tagfs_ftruncate(const char *, off_t, struct fuse_file_info *) {
	return 0;
};

int tagfs_readlink(const char *, char *, size_t){
	cout << "readlink" << endl;
	return -1;
};

int tagfs_symlink(const char *, const char *){
	cout << "symlink" << endl;
	return -1;
};
